home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / CALib & You… / Source / CASample / CAS_Main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-07  |  7.9 KB  |  371 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        CAS_Main.c
  3.  
  4.     Contains:    Main, init, and event loop.
  5.  
  6.     Written by:    David H Nelson
  7.  
  8.     Copyright © 1993-1995 ComponentWorks, All rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <12>     05/19/95    SJF        Allow null/idle events to be propagated  
  13.          ---------------
  14.  
  15.          <7>     05/13/95    RB        As Per the CALib pre-alpha code review, changed
  16.                                      main to call CAUtil_IsCALibPresent()
  17.                                      to determine CALib availablity.  
  18.          ---------------
  19.          <6>     04/22/95    RB        Changed main event loop to contain only one
  20.                                     call to CADispatchEvent()
  21.          ---------------
  22.          <5>     02/25/95    RB        Hacked up main event loop to pass events
  23.                                     to the OpenDoc dispatcher
  24.          ---------------
  25.          <4>     01/23/95    SJF        Add CALib support
  26.          ---------------
  27.          <3>     01/20/95    RB        Removed call to appAdjustCursor from the
  28.                                     main event loop.  It was being called everytime
  29.                                     through, now it's only called when a mouseMovedMessage
  30.                                     occurs.
  31.          ---------------
  32.          <2>     01/17/95    DAS        changed all FrontWindow() calls to
  33.                                      App_GetFrontDocWindow() to account for
  34.                                      floating windows.
  35.                  01/15/95    DAS        hacked in ToolWindow event handling in one location
  36.                   12/22/94    DHN        Created from existing Light software 
  37.                                      application.
  38.          ---------------
  39.          <1>     11/20/94    DHN        Created.
  40. */
  41.  
  42. #define _MAIN_
  43.  
  44. #ifdef USE_CALIB
  45. #include "CALib.h"
  46. #include "CAS_CAUtil.h"
  47. #endif
  48.  
  49. #include "CAS_Globals.h"
  50. #include "CAS_Misc.h"
  51. #include "CAS_Main.h"
  52. #include "CAS_App.h"
  53. #include "CAS_Event.h"
  54. #include "CAS_ToolPalette.h"
  55. #include "CAS_Menu.h"
  56.  
  57. #include "TraceLog.h"
  58.  
  59. //----------------------------------------------------------------------
  60. //     local prototypes
  61.  
  62. #if defined(__cplusplus)
  63. extern "C"
  64. {
  65. #endif
  66.  
  67. static void doEventLoop( void );
  68. static void InitMacintosh( void );
  69. static void InitGlobals( void );
  70. static void DisposeGlobals( void );
  71. static Boolean checkCALibGestalt(void);
  72.  
  73. #if defined(__cplusplus)
  74. }
  75. #endif
  76.  
  77. //---------------------------------------------------------------------------
  78. // Main event loop. Keeps looping until the users decides to quit. Dispatches each
  79. // event type to the appropriate handler after setting gOptionPressed, gShiftPressed
  80. // gCmdPressed, and gControlPressed. Calls the doIdle routine, appAdjustCursor,
  81. // and adjustMenus on each loop.
  82.  
  83. static void doEventLoop( void )
  84. {
  85. EventRecord        theEvent;
  86. WindowPtr        theWindow;
  87. Boolean            eventHandled;
  88. CAEventInfo        eventInfo;
  89. Boolean            result;
  90.  
  91.     /* was FrontWindow() */
  92.     theWindow = App_GetFrontDocWindow();
  93.  
  94.     // use theEvent.where (the last mouse position in global coords).
  95.     App_AdjustCursor( theWindow, theEvent.where, gMouseRgn );
  96.  
  97.     while (!gExitFlag)
  98.     {
  99.         Event_DoIdle();
  100.  
  101.         /* was FrontWindow() */
  102.         theWindow = App_GetFrontDocWindow();
  103.  
  104.         // use theEvent.where (the last mouse position in global coords).
  105.         //appAdjustCursor(theWindow, theEvent.where, gMouseRgn);
  106.         //Menu_AdjustMenus( theWindow );
  107.  
  108.         
  109.         result = WaitNextEvent( everyEvent, &theEvent, gSleepTime, gMouseRgn );
  110.         
  111.         {
  112.         
  113.             if (ToolPalette_IsEvent( nil, &theEvent ))
  114.                 if (ToolPalette_DoEvent( nil, &theEvent ))
  115.                     continue;
  116.  
  117.             gOptionPressed = ((theEvent.modifiers & optionKey) != 0);
  118.             gShiftPressed = ((theEvent.modifiers & shiftKey) != 0);
  119.             gCmdPressed = ((theEvent.modifiers & cmdKey) != 0);
  120.             gControlPressed = ((theEvent.modifiers & controlKey) != 0);
  121.             gSleepTime = CAGetSleepTime();
  122.             
  123.  
  124. #ifdef USE_CALIB
  125.             if (gCALibExists)
  126.             {
  127.                 eventHandled = CADispatchEvent( &theEvent, &eventInfo );
  128.                 
  129.                 if (eventHandled)
  130.                     continue;
  131.  
  132.             }
  133. #endif
  134.  
  135.             switch (theEvent.what)
  136.             {
  137.             
  138. #ifdef USE_CALIB
  139.  
  140.                 case kCAEvtMouseDownEmbedded:
  141.                     eventHandled = CAUtil_HandleMouseDownEmbedded (&theEvent, &eventInfo);
  142.                     break;
  143.                     
  144.                 case kCAEvtMouseUpEmbedded:
  145.                     eventHandled = CAUtil_HandleMouseUpEmbedded (&theEvent, &eventInfo);
  146.                     break;
  147.                     
  148.                 case kCAEvtMouseUpBorder:
  149.                     break;
  150.                     
  151.                 case kCAEvtMouseDownBorder:
  152.                     eventHandled = CAUtil_HandleMouseDownBorder (&theEvent, &eventInfo);
  153.                     break;
  154.                     
  155.                 case kCAEvtBGMouseDownEmbedded:
  156.                     eventHandled = CAUtil_HandleMouseDownEmbedded (&theEvent, &eventInfo);
  157.                     break;
  158.  
  159. #endif
  160.  
  161.                 case nullEvent:
  162.                     Event_HandleNullEvent( &theEvent );
  163.                     break;
  164.  
  165.                 case mouseDown:
  166.                     Event_HandleMouseDownEvent( &theEvent );
  167.                     break;
  168.  
  169.                 case mouseUp:
  170.                     Event_HandleMouseUpEvent( &theEvent );
  171.                     // Rick - Added this to force a mouseMovedMessage
  172.                     // So the cursor get updated
  173.                     SetRectRgn( gMouseRgn, 0, 0, 1, 1 );
  174.                     break;
  175.  
  176.                 case keyDown:
  177.                     Event_HandleKeyDownEvent( &theEvent );
  178.                     break;
  179.  
  180.                 case autoKey:
  181.                     Event_HandleAutoKeyEvent( &theEvent );
  182.                     break;
  183.  
  184.                 case keyUp:
  185.                     Event_HandleKeyUpEvent( &theEvent );
  186.                     break;
  187.  
  188.                 case activateEvt:
  189.                     Event_HandleActivateEvent( &theEvent );
  190.                     break;
  191.  
  192.                 case updateEvt:
  193.  
  194.                     // After CADispatchEvent the update region is
  195.                     // the original minus any area used by embedded parts
  196.  
  197.                     Event_HandleUpdateEvent( &theEvent );
  198.  
  199.  
  200.                     break;
  201.  
  202.                 case kHighLevelEvent:
  203.  
  204.                     Event_HandleHighLevelEvent( &theEvent );
  205.                     break;
  206.  
  207.                 case osEvt:
  208.  
  209.                     Event_HandleOSEvent( &theEvent );
  210.                     break;
  211.  
  212.                 case diskEvt:
  213.                     Event_HandleDiskEvent( &theEvent );
  214.                     break;
  215.  
  216.                 default:
  217.                     break;
  218.             }
  219.             
  220.         }
  221.         
  222.         
  223.         //SectRgn(gMouseRgn, CAGetMouseRegion(), gMouseRgn);
  224.         
  225.     }
  226.     
  227.  
  228. }
  229.  
  230.  
  231. //---------------------------------------------------------------------------
  232. // Initialize all of the managers and set the gestalt theDoc.
  233.  
  234. static void InitMacintosh( void )
  235. {
  236. EventRecord        anyEvent;
  237. short            i;
  238.  
  239.     MaxApplZone();
  240.     InitGraf( &qd.thePort );
  241.     InitFonts();
  242.     InitWindows();
  243.     InitMenus();
  244.     TEInit();
  245.     InitDialogs( 0L );
  246.  
  247.     // we must call EventAvail 3 times so the process mgr knows that
  248.     // we're alive and will make the Scrap purgable.
  249.     for (i=0; i<3; i++)
  250.         EventAvail(everyEvent, &anyEvent);
  251.  
  252.     // flush up to the next null event.
  253.     FlushEvents( everyEvent, nullEvent );
  254.     InitCursor();
  255. }
  256.  
  257. //---------------------------------------------------------------------------
  258. // Initialize all theDoc.
  259.  
  260. static void InitGlobals( void )
  261. {
  262.     gExitFlag = false;
  263.     gInBackground = false;
  264.  
  265.     gOptionPressed = false;
  266.     gShiftPressed = false;
  267.     gCmdPressed = false;
  268.     gControlPressed = false;
  269.     gMouseUpTickCount = 0;
  270.     gMouseUpPoint.h = gMouseUpPoint.v = 0;
  271.  
  272.     gSleepTime = GetCaretTime();
  273.     gMouseRgn = NewRgn();
  274.  
  275.     gPrefRefNum = 0;
  276.     gPrefChanged = false;
  277.  
  278.     gWindowCount = 0;
  279. }
  280.  
  281. //---------------------------------------------------------------------------
  282. // dispose all theDoc allocated in initGlobals.
  283.  
  284. static void DisposeGlobals( void )
  285. {
  286.     if (gMouseRgn != nil)
  287.     {
  288.         DisposeRgn( gMouseRgn );
  289.         gMouseRgn = nil;
  290.     }
  291. }
  292.  
  293. //---------------------------------------------------------------------------
  294. // This routine will check the CALib gestalt version
  295.  
  296. static Boolean checkCALibGestalt(void)
  297. {
  298. #if 0
  299.     OSErr    errStatus;
  300.     long    result;
  301.  
  302.     errStatus = Gestalt( gestaltCALibVersion, &result );
  303.  
  304.     return( (errStatus == noErr) && (result >= CAUtil_VERSION_1) );
  305. #else
  306.     return(true);
  307. #endif
  308. }
  309.  
  310. //---------------------------------------------------------------------------
  311. // Main entry point for application. Clears the theDoc. Calls InitMacintosh, 
  312. // setUpMenus, and if App_Init returns noErr calls doEventLoop. Calls appQuit 
  313. // when ready to quit.
  314.  
  315. void main( void )
  316. {
  317.     InitMacintosh();            // Initialize all MacOS Managers
  318.  
  319.     InitGlobals();                // Initialize app’s global variable
  320.  
  321.     InitTools();                // Initialize our utility routines
  322.  
  323. #if TRACING
  324.     Trace_StartLog(NULL, true);
  325. #endif
  326.  
  327. #ifdef USE_CALIB
  328. {
  329.     // Check to see if CALib exists.
  330.     gCALibExists = CAUtil_IsCALibPresent();
  331.     
  332.     if (gCALibExists)
  333.     {
  334.     
  335.         OSErr        theErr;
  336.  
  337.         theErr = CAUtil_InitCALib();
  338.  
  339.         if (theErr)
  340.             ;
  341.             
  342.     }
  343. }
  344. #endif
  345.  
  346.     Menu_SetUpMenus();            // Set up the app’s menu's
  347.  
  348.     if (App_Init() == noErr)    // Let the app initialize anything it needs
  349.     {
  350.         doEventLoop();            // Here we go
  351.  
  352. #ifdef USE_CALIB
  353.     if (gCALibExists)
  354.     {
  355.         OSErr        theErr;
  356.         theErr = CAUtil_ShutdownCALib();
  357.  
  358.     }
  359. #endif
  360.  
  361. #if TRACING
  362.     Trace_StopLog();
  363. #endif
  364.  
  365.         App_Quit();                // Let the app clean up before quit
  366.         DisposeGlobals();
  367.     }
  368.     
  369. }
  370.  
  371.